/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import entity.Employee;

/**
 *
 * @author zheng
 */
public class EmployeeDaoJTableImpl implements EmployeeDao{
    
    private JTable jTable;

    public EmployeeDaoJTableImpl(JTable jTable) {
        this.jTable = jTable;
    }
    
    
    
    @Override
    public boolean save(Employee emp) {
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        String[] strs = convertEmployeeToStringArr(emp);
        model.addRow(strs);
        return true;
    }

    private String[] convertEmployeeToStringArr(Employee emp) {
       String[] strs =new String[3];
       strs[0] = emp.getId()+"";
       strs[1] = emp.getName();
       strs[2] = emp.getAge()+"";
       return strs;
    }

    @Override
    public boolean deleteByIndex(int index) {
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();        
        model.removeRow(index);
        return true;
    }
    
}
